package fire.and.near.challenger.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentActivity;
import fire.and.near.challenger.database.DatabaseActivity;
import fire.and.near.challenger.database.DatabaseAsyncTask;
import fire.and.near.challenger.database.DatabaseAsyncTask.DatabaseQueryOptions;
import fire.and.near.challenger.database.TableConstants.ChallengesTable;
import fire.and.near.challenger.util.Consts;

public class ChallengeDialogFragment extends DialogFragment {

	private Integer mChallengeId = 0;
	private String mText = null;
	private DatabaseQueryOptions mOption = null;

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final FragmentActivity actvitiy = getActivity();
		AlertDialog.Builder builder = new AlertDialog.Builder(actvitiy);
		builder.setTitle(mText)
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								new DatabaseAsyncTask(actvitiy, mOption)
										.execute(mChallengeId);
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
						((DatabaseActivity) actvitiy).setDatabaseWorking(false);
					}
				});

		return builder.create();
	}

	@Override
	public void setArguments(Bundle args) {
		this.mChallengeId = args.getInt(ChallengesTable.ID);
		this.mText = args.getString(Consts.QUESTION);
		int option = args.getInt(Consts.OPTION);
		if (option == 1) {
			this.mOption = DatabaseQueryOptions.DECLINE;
		} else if (option == 2) {
			this.mOption = DatabaseQueryOptions.DELETE_CHALLENGE;
		}
	}
}
